package com.sgai.zero_robot.ui.history;

import static com.sgai.zero_robot.utils.LogUtil.rootPath;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.sgai.zero_robot.base.BaseActivity;
import com.sgai.zero_robot.base.BaseAdapter;
import com.sgai.zero_robot.base.BaseViewModel;
import com.sgai.zero_robot.database.ZeroDetectionRepository;
import com.sgai.zero_robot.databinding.ActivityZeroHistoryBinding;
import com.sgai.zero_robot.pojo.InstallInfo;
import com.sgai.zero_robot.utils.ThreadPoolUtils;
import com.sgai.zero_robot.widget.TableView;

import java.util.List;


public class ZeroHistoryActivity extends BaseActivity<ActivityZeroHistoryBinding, BaseViewModel> implements View.OnClickListener {
    private ZeroListAdapter zeroListAdapter ;
    private LoadRunnable loadRunnable;
    private DeleteRunnable deleteRunnable;
    private ExportRunnable exportRunnable;

    @Override
    public void onClick(View view) {
        if (view == mBinding.ivCallback){
            finish();
        }else if (view == mBinding.tvDelete){
            if (zeroListAdapter.getData().size()<=0){
                return;
            }
            showLoading("正在删除...");
            InstallInfo installInfo = zeroListAdapter.getData().remove(zeroListAdapter.getCurrentIndex());
            deleteRunnable = new DeleteRunnable(zeroDetectionRepository,installInfo.lineName,installInfo.towerName,installInfo.install, new RunnableCallBack<Boolean>() {
                @Override
                public void onResult(Boolean b) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dismissLoading();
                            zeroListAdapter.setData(zeroListAdapter.getData());
                            if (zeroListAdapter.getData().size()>0){
                                zeroListAdapter.setCurrentIndex(0);
                                mBinding.tableView.setDate(zeroListAdapter.getData().get(0));
                            }else{
                                mBinding.tableView.clearDate();
                            }
                        }
                    });
                }
            });
            ThreadPoolUtils.getInstance().addSerialTask(deleteRunnable);
        }else if (view == mBinding.tvExport){
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != android.content.pm.PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
            }else{
                if (zeroListAdapter.getData() == null||zeroListAdapter.getData().size()<=0){
                    return;
                }
                showLoading("正在导出...");
                exportRunnable = new ExportRunnable( zeroListAdapter.getData().get(zeroListAdapter.getCurrentIndex()),rootPath, new RunnableCallBack<String>() {
                    @Override
                    public void onResult(String b) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dismissLoading();
                                if (TextUtils.isEmpty(b)){
                                    showToast("文档导出失败");
                                }else{
                                    showToast("文档导出成功 "+b);
                                }
                            }
                        });
                    }
                });
                ThreadPoolUtils.getInstance().addSerialTask(exportRunnable);
            }
        }
    }

    @Override
    public void initViews(Bundle savedInstanceState) {
        zeroListAdapter = new ZeroListAdapter(this);
        mBinding.recyLeft.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyLeft.setAdapter(zeroListAdapter);
        mBinding.tableView.setTableCallback(new TableView.TableCallback() {
            @Override
            public void onFInish() {
                dismissLoading();
            }
        });
        zeroDetectionRepository = new ZeroDetectionRepository();
        zeroListAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<InstallInfo>() {
            @Override
            public void onItemClick(int position, InstallInfo installInfo) {
                int currentIndex = zeroListAdapter.getCurrentIndex();
                if (currentIndex == position) {
                    return;
                }
                zeroListAdapter.setCurrentIndex(position);
                zeroListAdapter.notifyItemChanged(currentIndex);
                zeroListAdapter.notifyItemChanged(position);
                mBinding.tableView.setDate(installInfo);
            }
        });
        mBinding.ivCallback.setOnClickListener(this);
        mBinding.tvDelete.setOnClickListener(this);
        mBinding.tvExport.setOnClickListener(this);
    }
    private ZeroDetectionRepository zeroDetectionRepository;
    @Override
    public void loadData() {
        loadRunnable = new LoadRunnable(zeroDetectionRepository, new RunnableCallBack<List<InstallInfo>>() {
            @Override
            public void onResult(List<InstallInfo> installInfos) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (installInfos == null||installInfos.size()<=0){
                            dismissLoading();
                            return;
                        }
                        zeroListAdapter.setData(installInfos);
                        mBinding.tableView.setDate(installInfos.get(0));
                    }
                });
            }
        });
        showLoading("正在加载...");
        ThreadPoolUtils.getInstance().addSerialTask(loadRunnable);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (loadRunnable != null) {
            ThreadPoolUtils.getInstance().removeParallelTask(loadRunnable);
        }
        if (deleteRunnable != null) {
            ThreadPoolUtils.getInstance().removeParallelTask(deleteRunnable);
        }
        if (exportRunnable != null) {
            ThreadPoolUtils.getInstance().removeParallelTask(exportRunnable);
        }
    }
}